package com.lulin.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lulin.mall.product.entity.CategoryEntity;
import com.lulin.mall.product.service.CategoryService;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.R;


/**
 * 商品三级分类
 *
 * @author lulin
 * @email 2032327049@qq.com
 * @date 2022-09-21 14:25:13
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 查询所有的类别数据，将数据封装为树形结构，便于前端使用
     *
     * @param params    查询参数 map 集合
     * @return
     */
    @GetMapping("/listTree")
    public R listTree(@RequestParam Map<String, Object> params) {
        //查询所有的类别数据，将数据封装为树形结构，便于前端使用
        List<CategoryEntity> list = categoryService.queryPageWithTree(params);
        return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 根据类目id，更新品牌-类别关联表中的类别名称
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        //categoryService.updateById(category);
        categoryService.updateDetail(category);

        return R.ok();
    }

    /**
     * 生成的批量删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

    /**
     * 批量删除（逻辑）
     */
    @RequestMapping("/logicDelete")
    public R removeCategoryByIds(@RequestBody Long[] catIds) {
        //逻辑批量删除
        categoryService.removeCategoryByIds(Arrays.asList(catIds));
        return R.ok();
    }

    /**
     * 批量更新
     *
     * @param categoryEntity 前端提交的数据封装对象
     * @return 成功的状态码
     */
    @RequestMapping("/updateBatch")
    public R updateBatch(@RequestBody CategoryEntity[] categoryEntity) {
        categoryService.updateBatchById(Arrays.asList(categoryEntity));
        return R.ok();
    }


}
